package com.xbongbong.pro.statistic.help.strategy.chart.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.domain.entity.ChartEntity;
import com.xbongbong.pro.service.toolbox.statistic.help.ChartDtoHelp;
import com.xbongbong.pro.statistic.enums.ChartTypeEnum;
import com.xbongbong.pro.statistic.help.strategy.chart.ChartStrategyService;
import com.xbongbong.pro.statistic.model.ChartModel;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultBaseDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ChartResultDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.toolbox.help.BasicHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 系统图表
 * @author xinpeng.jiang
 * @version V1.0
 * @date 2021/5/17 10:00
 * @since V1.0
 */
@Service
public class SystemChartStrategyServiceImpl implements ChartStrategyService {
    @Resource
    private ChartModel chartModel;
    @Resource
    private ChartDtoHelp chartDtoHelp;

    @Override
    public ChartEntity getByKey(Long chartId, String corpid) {
        return chartModel.getByKey(chartId, corpid);
    }

    @Override
    public List<ChartEntity> getChartList(ChartResultBaseDTO chartResultBaseDTO) throws XbbException {
        //处理chartIdIn，把系统图表和自定义图表分装到sysIdIn、customIdIn
        chartDtoHelp.dealWithIdIn(chartResultBaseDTO);
        //塞入map
        Map<String, Object> map = BeanUtil.convertBean2Map(chartResultBaseDTO, true);
        map.put("del", DelEnum.NORMAL.getDel());
        map.put("idIn", chartResultBaseDTO.getSysIdIn());
        return (List<ChartEntity>) BasicHelper.findEntitysByImitatePage(map, chartModel, BasicConstant.THREE_HUNDRED);
    }

    @Override
    public List<ChartEntity> getChartList(Map<String, Object> map) throws XbbException {
        map.put("del", DelEnum.NORMAL.getDel());
        return chartModel.findEntitys(map);
    }

    @Override
    public List<ChartEntity> getPkChartList(ChartResultDTO chartResultDTO) throws XbbException {
        //处理chartIdIn，把系统图表和自定义图表分装到sysIdIn、customIdIn
        chartDtoHelp.dealWithIdIn(chartResultDTO);
        //查找图表列表
        Map<String, Object> map = BeanUtil.convertBean2Map(chartResultDTO, true);
        map.put("chartType", ChartTypeEnum.PK.getCode());
        map.put("del", DelEnum.NORMAL.getDel());
        map.put("idIn", chartResultDTO.getSysIdIn());
        return chartModel.findEntitys(map);
    }

    @Override
    public Integer updateChart(ChartEntity entity) throws XbbException {
        return chartModel.update(entity);
    }

    @Override
    public void batchUpdateNumFormat(ChartResultDTO chartResultDTO) throws XbbException {
        //处理chartIdIn，把系统图表和自定义图表分装到sysIdIn、customIdIn
        chartDtoHelp.dealWithIdIn(chartResultDTO);
        // 保存
        chartModel.batchUpdateNumFormat(chartResultDTO.getCorpid(), chartResultDTO.getSysIdIn(), chartResultDTO.getNumFormat());
    }
}
